<?php
  $s=get_bloginfo('name');
  $tags = wp_get_post_tags($post->ID);
  $description = get_option('ini_description');
  $keywords = get_option('ini_keywords');

if (is_home()){ echo '<title>'.$s.' | '.get_bloginfo('description').'</title>';} else { echo '<title>'; if (is_search()){ echo '搜索结果';} if (is_single()){ echo trim(wp_title('',0)); if (get_query_var('page')){ echo '-第'.get_query_var('page').'页';} } if (is_page()){ echo trim(wp_title('',0));} if (is_category()){ single_cat_title();} if (is_month()){ the_time('F');} if (function_exists('is_tag')){ if (is_tag()){ single_tag_title("", true);} } echo ' | '.$s.'</title>';}

if(is_home()){$description = $description;$keywords = $keywords;}
elseif (is_category()){
$keywords = single_cat_title('', false);
    if(category_description()==''){ $description = $description; }
    else {
        $description= category_description();
        $description=str_replace("<p>","",str_replace("</p>","",$description));
    }
}
elseif (have_posts()){
    if ($post->post_excerpt){
        $description  = $post->post_excerpt;
    } else
    if (get_post_meta($post->ID,'description_value',true)){
        $description = get_post_meta($post->ID,'description_value',true);
    } else {
   if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){
    $post_content = $result['1'];
    } else {
    $post_content_r = explode("\n",trim(strip_tags($post->post_content)));
    $post_content = $post_content_r['0'];
    }
         $judge = utf8_str($post_content,0,220);
         if (($judge)!=='') { $description = $judge; }
    }
    if (get_post_meta($post->ID,'keywords_value',true)){
        $keywords = get_post_meta($post->ID,'keywords_value',true);
    }
    elseif ($tags !== ''){
        $keywords = '';
        foreach ($tags as $tag ){
            $keywords = $keywords . $tag->name . ",";
        }
    }
}
?>

<meta name="description" content="<?php echo trim($description); ?>" />
<meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />
